package com.lu.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lu.model.entity.SysStore;
import com.lu.model.request.ProcessApprovalUpdateInfoRequest;
import com.lu.model.request.StoreAddRequest;
import com.lu.model.request.StoreQueryRequest;
import com.lu.model.response.Result;
import com.lu.service.SysApprovalService;
import com.lu.service.SysStoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 门店 前端控制器
 * </p>
 *
 * @author zhanglu
 * @since 2022-07-31
 */
@RestController
@RequestMapping("/sys-store")
@RequiredArgsConstructor
public class SysStoreController {

  private final SysStoreService storeService;
  private final SysApprovalService approvalService;

  @PreAuthorize("hasAuthority('/sys-store/page-list')")
  @PostMapping("/page-list")
  public Result<Page<SysStore>> pageList(@RequestBody StoreQueryRequest request){
    return Result.success(storeService.pageList(request));
  }

  @PostMapping("/list")
  public Result<List<SysStore>> list(@RequestBody StoreQueryRequest request){
    return Result.success(storeService.storeList(request));
  }

  @PreAuthorize("hasAuthority('/sys-store/get')")
  @GetMapping("/get/{id}")
  public Result<SysStore> get(@PathVariable Long id){
    return Result.success(storeService.getById(id));
  }

  @PreAuthorize("hasAuthority('/sys-store/add')")
  @PostMapping("/add")
  public Result<Long> add(@RequestBody @Validated StoreAddRequest request){
    return Result.success(storeService.addStore(request));
  }

  @PreAuthorize("hasAuthority('/sys-store/update')")
  @PostMapping("/update")
  public Result<SysStore> update(@RequestBody SysStore store){
    return Result.success(storeService.updateStore(store));
  }

  /**
   * 审批修改 - 修改审核信息，将status设置为 审核中，即重新发起审核流程
   *  - 只有如下场景才可以修改
   *  - @see {@link com.lu.model.enums.ApprovalStatusEnum#UN_AUDITED} {@link com.lu.model.enums.ApprovalStatusEnum#NO_PASS}
   * @param store
   * @return
   */
  @PreAuthorize("hasAuthority('/sys-store/update-approval')")
  @PostMapping("/update-approval")
  public Result<SysStore> updateApproval(@RequestBody SysStore store){
    ProcessApprovalUpdateInfoRequest request = new ProcessApprovalUpdateInfoRequest();
    request.setTableName("sys_store");
    request.setObjId(store.getId());
    store.setId(null);
    store.setStatus(null);
    store.setCreateUserId(null);
    store.setCreateTime(null);
    request.setParams(JSONUtil.parseObj(store));
    return Result.success(approvalService.updateInfo(request));
  }

  @PreAuthorize("hasAuthority('/sys-store/delete')")
  @PostMapping("/delete/{id}")
  public Result<String> delete(@PathVariable Long id){
    return Result.success(storeService.delete(id));
  }

  /**
   * 门店导入
   *  - 门店名称唯一，若门店存在，则忽略，不导入
   *  - 账号名称默认为：品牌名字母首拼缩写 + '_' + 门店名字母首拼缩写 + 2位随机数字
   *  - 密码默认为 111111
   * @param file
   * @return
   * @throws IOException
   */
  @PostMapping("/import")
  public Result<List<String>> importStore(@RequestParam MultipartFile file) throws IOException {
    return Result.success(storeService.importStore(file.getOriginalFilename(), file.getInputStream()));
  }

  /**
   * 当前登录用户的（商户） 线下门店列表
   */
  @GetMapping("/offline-list/login-user")
  private Result<List<SysStore>> loginUserOfflineStores(){
    return Result.success(storeService.loginUserOfflineStores());
  }

}
